package com.ruoyi.project.system.file.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.file.domain.UploadFile;
import com.ruoyi.project.system.file.service.IUploadFileService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 资源文件Controller
 * 
 * @author f_baisi
 * @date 2020-06-06
 */
@Controller
@RequestMapping("/system/file")
public class UploadFileController extends BaseController
{
    private String prefix = "system/file";

    @Autowired
    private IUploadFileService uploadFileService;

    @RequiresPermissions("system:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询资源文件列表
     */
    @RequiresPermissions("system:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UploadFile uploadFile)
    {
        startPage();
        List<UploadFile> list = uploadFileService.selectUploadFileList(uploadFile);
        return getDataTable(list);
    }

    /**
     * 导出资源文件列表
     */
    @RequiresPermissions("system:file:export")
    @Log(title = "资源文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UploadFile uploadFile)
    {
        List<UploadFile> list = uploadFileService.selectUploadFileList(uploadFile);
        ExcelUtil<UploadFile> util = new ExcelUtil<UploadFile>(UploadFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 新增资源文件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资源文件
     */
    @RequiresPermissions("system:file:add")
    @Log(title = "资源文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UploadFile uploadFile)
    {
        return toAjax(uploadFileService.insertUploadFile(uploadFile));
    }

    /**
     * 修改资源文件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        UploadFile uploadFile = uploadFileService.selectUploadFileById(id);
        mmap.put("uploadFile", uploadFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存资源文件
     */
    @RequiresPermissions("system:file:edit")
    @Log(title = "资源文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UploadFile uploadFile)
    {
        return toAjax(uploadFileService.updateUploadFile(uploadFile));
    }

    /**
     * 删除资源文件
     */
    @RequiresPermissions("system:file:remove")
    @Log(title = "资源文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(uploadFileService.deleteUploadFileByIds(ids));
    }

    /**
     * 删除垃圾数据
     * @return
     */
    @RequiresPermissions("system:file:remove")
    @Log(title = "资源文件", businessType = BusinessType.DELETE)
    @PostMapping( "/removegarbagedata")
    @ResponseBody
    public AjaxResult removeGarbageData(){
        //查询出所有垃圾文件
        List<UploadFile> uploadFiles = this.uploadFileService.selectGarbageData();
        if(uploadFiles.size()==0){
            return AjaxResult.success();
        }
        //删除数据库数据
        this.uploadFileService.deleteGarbageData();
        for(UploadFile uploadFile:uploadFiles){
            //删除本地文件
            FileUploadUtils.delete(uploadFile.getFileRealPath());
        }
        return AjaxResult.success();
    }
}
